package com.vue.admin.controller;

import com.github.pagehelper.PageInfo;
import com.vue.admin.entity.SysRole;
import com.vue.admin.entity.SysRoleExample;
import com.vue.admin.service.SysRoleService;
import com.vue.admin.util.PageHelper2;
import com.vue.admin.util.ResponseState;
import com.vue.admin.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/sysRole")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleServieImpl;

    @RequestMapping(value = "/select")
    public PageInfo select(@RequestBody Map map) throws Exception {
        SysRoleExample example = new SysRoleExample();
        PageHelper2.startPage(map);
        List<SysRole> list = sysRoleServieImpl.selectByCondition(example);
        PageInfo<SysRole> appsPageInfo = new PageInfo<>(list);
        return appsPageInfo;//PageUtil.pageVO();

    }

    @RequestMapping(value = "/all")
    public List<SysRole> all() throws Exception {
        return sysRoleServieImpl.selectByCondition(null);

    }


    @RequestMapping(value = "/del")
    public ResponseState del(@RequestBody SysRole sysRole) {
        if (StringUtil.isEmpty(sysRole.getId())) {
            return new ResponseState(false, "请选择要删除的角色！");
        }
        sysRoleServieImpl.delete(sysRole.getId());
        return new ResponseState();
    }

    @RequestMapping(value = "/save")
    public ResponseState save(@RequestBody SysRole sysRole) {
        if (StringUtil.isEmpty(sysRole.getCode())) {
            return new ResponseState(false, "编码不能为空！");
        }
        sysRoleServieImpl.save(sysRole);
        return new ResponseState();
    }
}
